<?php

namespace app\index\model;

use app\index\util\Taoke;
use think\Model;
use think\Request;

class UtaoSearch extends Model
{
    public function search($keyword, $query = [])
    {
        $tk = new Taoke;
        $data = [
                'q' => $keyword,
                'has_coupon'=>'true',
                'ip'=>Request::instance()->ip(),
                'include_pay_rate30'=>'false',
                'include_good_rate'=>'false',
                'include_rfd_rate'=>'false',
            ] + $query;
//        var_dump($data);
        $res = $tk->search_coupon($data);
        if (isset($res['result_list'])) {
            $this->log_word($keyword);
            return $res;
        }
        $this->error = isset($res['sub_msg'])?$res['sub_msg']:'';
        return false;
    }

    public function log_word($keyword)
    {
        $data = [
            'keyword' => $keyword,
            'ip' => Request::instance()->ip(),
            'create_time' => Request::instance()->time(),
        ];
        // 排重
        $map = ['ip' => Request::instance()->ip(), 'keyword' => $keyword, 'create_time' => ['GT', strtotime(date('Y-m-d'))]];
        $find = $this->get($map);
        if ($find) return false;
        $this->save($data);
    }
}